/*
 * Licensed Materials - Property of tenxcloud.com
 * (C) Copyright 2018 TenxCloud. All Rights Reserved.
 * 2018-06-13  @author lizhen
 */

package cluster

import (
	"github.com/golang/glog"
	"github.com/jinzhu/gorm"
)

type model struct {
	ID          string `gorm:"column:id"`
	Host        string `gorm:"column:api_host"`
	Token       string `gorm:"column:api_token"`
	Protocol    string `gorm:"column:api_protocol"`
	ClusterName string `gorm:"column:name" json:"clusterName"`
}

func (model) TableName() string {
	return "tenx_clusters"
}

func (c model) GetID() string {
	return c.ID
}

func (c model) GetHost() string {
	return c.Host
}

func (c model) GetToken() string {
	return c.Token
}

func (c model) GetProtocol() string {
	return c.Protocol
}

func (c model) GetClusterName() string {
	return c.ClusterName
}

func FindOneByClusterID(clusterID string, db *gorm.DB) (cluster *model, err error) {
	var c model
	if err = db.Where("id = ?", clusterID).First(&c).Error; err != nil {
		return
	}
	cluster = &c
	return
}

func Find(db *gorm.DB) (clusters []model, err error) {
	if err := db.Find(&clusters).Error; err != nil {
		glog.Error(err)
		return nil, err
	}
	return clusters, nil
}
